<?php
/**
 * Created by PhpStorm.
 * User: 69571
 * Date: 2019/8/9
 * Time: 9:05
 */
namespace app\models;
use Yii;
use yii\base\Model;

class Rbac extends  Model{
    /**
     * @return array
     * @throws \yii\db\Exception
     *  查找 所有的角色
     */
    public function Role(){
        $Role_arr = Yii::$app->db->createCommand("SELECT * FROM role")->queryAll();
        return $Role_arr;
    }

    /**
     * @param $data
     * @return false|string
     * @throws \yii\db\Exception
     *  添加新管理员
     */
    public function insertUsers($data){
        $u_name = $data['adminName'];
        $u_pwd = md5($data['password']);
        $u_address = $data['address'];
        $u_sex = $data['sex'];
        $u_tel  = $data['phone'];
        $u_email = $data['email'];
        $role_id = $data['adminRole'];
       $user_res =  Yii::$app->db->createCommand("INSERT INTO users (id,u_name,u_pwd,u_tel,u_address,u_email,u_sex) VALUES (null,'$u_name','$u_pwd','$u_tel','$u_address','$u_email','$u_sex') ")->execute();
        $user_id=Yii::$app->db->getLastInsertID();
       if($user_res){
           $add_res = Yii::$app->db->createCommand("INSERT INTO u_r VALUES ('$user_id','$role_id') ")->execute();
           if($add_res){
               return json_encode(['code'=>200,'msg'=>'添加成功']);
           }else{
               return json_encode(['code'=>500,'msg'=>'修改失败']);
           }
       }
    }

    /**
     * @return array
     * @throws \yii\db\Exception
     *  查看管理员列表信息
     */

    public function user_list(){
        $user_arr = Yii::$app->db->createCommand("SELECT s.*,r.role_name FROM users as s join u_r as ur on s.id = ur.u_id join role as r on ur.r_id =r.role_id  order by s.id")->queryAll();
        return $user_arr;
    }
    /**
     * @param $uid
     * @return array|false
     * @throws \yii\db\Exception
     *  查看单条管理员的信息
     */
    public function user_one($uid){
        $user_one = Yii::$app->db->createCommand("SELECT s.*,r.role_name,ur.*,r.role_id FROM users as s join u_r as ur on s.id = ur.u_id join role as r on ur.r_id =r.role_id WHERE s.id = $uid")->queryOne();
        return $user_one;
    }
    public function updateUser($data){
        $uid = $data['uid'];
        $u_name = $data['adminName'];
        $u_pwd = md5($data['password']);
        $u_address = $data['address'];
        $u_sex = $data['sex'];
        $u_tel  = $data['phone'];
        $u_email = $data['email'];
        $role_id = $data['adminRole'];
        /**
         * id,u_name,u_pwd,u_tel,u_address,u_email,u_sex
         */
        $updateRes = Yii::$app->db->createCommand("UPDATE users SET u_name = '$u_name',u_pwd = '$u_pwd',u_address='$u_address',u_email='$u_email',u_sex='$u_sex',u_tel='$u_tel' WHERE id =$uid")->execute();
        $updateRole = Yii::$app->db->createCommand("UPDATE u_r set r_id ='$role_id' WHERE u_id=$uid ")->execute();
        if($updateRes && $updateRole){
            return json_encode(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json_encode(['code'=>500,'msg'=>'修改失败']);
        }
    }
    /**
     *  修改管理员的状态
     */
    public function changeStatus($data){
        $uid = $data['uid'];
        $status = $data['status'];
        $changeRes = Yii::$app->db->createCommand("UPDATE users SET u_states='$status' WHERE id=$uid")->execute();
        if($changeRes){
            return json_encode(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json_encode(['code'=>400,'msg'=>'修改失败']);
        }
    }
    /**
     *  删除管理员
     *
     */
    public function userDel($data){
        $uid = $data['uid'];
        $userRes = Yii::$app->db->createCommand("DELETE FROM users WHERE id = $uid")->execute();
        $urRes = Yii::$app->db->createCommand("DELETE FROM u_r WHERE u_id = $uid")->execute();
        if($userRes && $urRes){
            return json_encode(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json_encode(['code'=>500,'msg'=>'删除失败']);
        }
    }
    /**
     *
     *  权限查询
     */
    /**
     * @return array
     * @throws \yii\db\Exception
     *
     */
    public function power_arr(){
        $power_arr = Yii::$app->db->createCommand("SELECT * FROM power")->queryAll();
        return $power_arr;
    }
     /**
      *
      *  查询出所有的角色
      */
     public function role_arr(){
         $role_arr = Yii::$app->db->createCommand("SELECT * FROM roel");
         return $role_arr;
     }

     /**
      *
      *  所有角色管理查询
      */
     public function admin_role_arr(){
         $admin_role_arr = Yii::$app->db->createCommand("SELECT * FROM users as u join u_r as r on u.id = r.u_id join role as q on r.u_id = q.role_id ")->queryAll();
         return $admin_role_arr;
     }
     /**
      *
      * 删除用户
      */
     public function deleteUsers($id){
         $delete_res  = Yii::$app->db->createCommand("DELETE FROM users WHERE id = $id")->execute();
                        Yii::$app->db->createCommand("DELETE FROM u_r WHERE u_id = $id")->execute();
         if($delete_res){
             return json_encode(['code'=>200,'msg'=>'删除成功']);
         }else{
             return json_encode(['code'=>500,'msg'=>'删除失败']);
         }
     }
     /**
      *
      * 查询出单用户对应的权限进行修改
      */
     public function role_one($id){
         $role_one = Yii::$app->db->createCommand("SELECT * FROM role as r join r_p as p on r.role_id = p.r_id join power as w on w.power_id = p.p_id where r.role_id = $id")->queryOne();
         return $role_one;
     }
     /*
      *
      * 修改角色信息
      */

     public function change_role($data){
         $role_name = $data['role_name'];
         $role_remarks = $data['role_remarks'];
         $power_id = $data['power_id'];
         $role_id = $data['role_id'];
         $change_res = Yii::$app->db->createCommand("UPDATE role SET  role_name='$role_name',role_remarks='$role_remarks' WHERE role_id=$role_id")->execute();
                Yii::$app->db->createCommand("UPDATE r_p SET p_id = $power_id WHERE r_id = $role_id")->execute();
                if($change_res){
                    return json_encode(['code'=>200,'msg'=>'修改成功']);
                }else{
                    return json_encode(['code'=>500,'msg'=>'修改失败']);
                }
     }
     /**
      * 根据搜索条件进行管理员信息搜索
      *
      */
     public function  search_admin($data){
         $begin_time = $data['datemin'];
         $end_time   = $data['datemax'];
         $admin_name=$data['admin_name'];
         $where = ' 1=1';
         if($begin_time!=''){
             $where.=" AND u_create_at > '$begin_time'";
         }
         if($end_time!=''){
             $where .= " AND u_create_at < '$end_time'";
         }
         if($admin_name!=''){
             $where .= " AND u_name like '%$admin_name%'";
         }
         $admin_arr = Yii::$app->db->createCommand("SELECT s.*,r.role_name FROM users as s join u_r as ur on s.id = ur.u_id join role as r on ur.r_id =r.role_id where $where")->queryAll();

         return $admin_arr;
     }

























































































}